from fastapi import APIRouter, Request, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import os

# 创建模板实例
templates = Jinja2Templates(directory="templates")

# 创建Web路由器
web_router = APIRouter(
    tags=["web"],
    responses={404: {"description": "Not found"}},
)

# 首页路由
@web_router.get("/", response_class=HTMLResponse)
async def index(request: Request):
    """
    渲染首页
    """
    return templates.TemplateResponse(
        "index.html", 
        {
            "request": request, 
            "version": "1.0.0",
            "environment": os.getenv("ENVIRONMENT", "development")
        }
    )

# 404页面
@web_router.get("/404", response_class=HTMLResponse)
async def not_found(request: Request):
    """
    渲染404页面
    """
    return templates.TemplateResponse(
        "404.html", 
        {"request": request}, 
        status_code=404
    ) 